package com.hzcx.app.simplechat.ui.chat.adapter;

import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.style.ImageSpan;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.ui.chat.bean.ChatQuickMsgBean;
import com.hzcx.app.simplechat.ui.moment.util.CenterAlignImageSpan;
import com.hzcx.app.simplechat.util.dp.DpUtils;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;

import java.util.List;

public class ChatQuickMsgManagerAdapter extends BaseQuickAdapter<ChatQuickMsgBean, BaseViewHolder> {

    private boolean isLeft = true;

    public ChatQuickMsgManagerAdapter(@Nullable List<ChatQuickMsgBean> data) {
        super(R.layout.rv_item_quick_msg_manager, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ChatQuickMsgBean item) {
        SwipeMenuLayout swipe_msg = helper.getView(R.id.swipe_menu);
        if (isLeft) {
            swipe_msg.setSwipeEnable(true);
            helper.setVisible(R.id.iv_move, false);
            helper.setVisible(R.id.tv_edit, true);
        } else {
            swipe_msg.setSwipeEnable(false);
            helper.setVisible(R.id.iv_move, true);
            helper.setVisible(R.id.tv_edit, false);
        }

        helper.setText(R.id.tv_content, item.getMaincontent());

        helper.addOnClickListener(R.id.tv_delete, R.id.tv_edit);

        if (item.getReply_images_arr() != null && item.getReply_images_arr().size() > 0) {
            helper.setTextColor(R.id.tv_content, mContext.getResources().getColor(R.color.color_2D7CFD));
            SpannableString sps = new SpannableString(item.getMaincontent() + "图片");
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.ic_icon_chat_qucik_img);
            drawable.setBounds(0, 0, DpUtils.dp2px(mContext, 20), DpUtils.dp2px(mContext, 17));
            //居中对齐imageSpan
            CenterAlignImageSpan imageSpan = new CenterAlignImageSpan(drawable);
            sps.setSpan(imageSpan, item.getMaincontent().length(), item.getMaincontent().length() + 2, ImageSpan.ALIGN_BASELINE);
            helper.setText(R.id.tv_content, sps);
        } else {
            helper.setTextColor(R.id.tv_content, mContext.getResources().getColor(R.color.color_282F3E));
        }
    }

    public void setIsLeft(boolean isLeft) {
        this.isLeft = isLeft;
        notifyDataSetChanged();
    }
}
